using System;
using System.Collections.Generic;
using System.Text;

namespace GarbageTruck.TemporalExpressions
{
    public class Difference : ITemporalExpression
    {
        #region CONSTRUCTORS

        public Difference()
        {
        }

        public Difference(ITemporalExpression firstExpression, ITemporalExpression secondExpression)
        {
            this.FirstExpression = firstExpression;
            this.SecondExpression = secondExpression;
        }

        #endregion CONSTRUCTORS

        #region FIELDS

        private ITemporalExpression m_FirstExpression;
        private ITemporalExpression m_SecondExpression;

        #endregion FIELDS

        #region PROPERTIES

        public ITemporalExpression FirstExpression
        {
            get { return m_FirstExpression; }
            set { m_FirstExpression = value; }
        }

        public ITemporalExpression SecondExpression
        {
            get { return m_SecondExpression; }
            set { m_SecondExpression = value; }
        }

        #endregion PROPERTIES

        #region METHODS

        public bool Contains(DateTime dateTime)
        {
            return (this.SecondExpression.Contains(dateTime) && !this.SecondExpression.Contains(dateTime));
        }

        public DateTime? GetNextOcurrence(DateTime dateTime)
        {
            DateTime firstNextOcurrence = this.FirstExpression.GetNextOcurrence(dateTime).Value;
            if (Contains(firstNextOcurrence) == false) firstNextOcurrence = GetNextOcurrence(firstNextOcurrence).Value;
            return firstNextOcurrence;
        }

        public DateTime? GetEventEnd(DateTime dateTime)
        {
            return dateTime;
        }

        public override string ToString()
        {
            return string.Format("[{0}]D[{1}]",
                                this.FirstExpression.ToString(),
                                this.SecondExpression.ToString());
        }

        #endregion METHODS
    }
}
